package com.zhy.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Token {
    public static String getJWTBuild(String loginName){
        //加密
        Map<String,Object> jwtmap=new HashMap<String,Object>();
        jwtmap.put("alg","HS256");
        jwtmap.put("typ","JWT");
        long str=System.currentTimeMillis()+1000*86400;//设置过期时间1个小时
        String token = JWT.create()
                .withHeader(jwtmap)
                .withExpiresAt(new Date(str))
                .withClaim("loginName",loginName)
                .sign(Algorithm.HMAC256("wqewqeqedacxafsf"));
        return token;
    }


    public static boolean jyJWT(String token){
        if (token==null){
            return false;
        }
        try {
            //解密
            JWTVerifier verifier  = JWT.require(Algorithm.HMAC256("wqewqeqedacxafsf")).build();
            verifier.verify(token);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }


}
